๐Ÿ‘ฉโ€๐Ÿ’ป 42893. ๋งค์นญ ์ ์ˆ˜

@Seongukbaek Posted June 05, 2022 ยท Updated June 05, 2022 ยท 3 min read

๋ฌธ์ œ

Programmers - ๋งค์นญ ์ ์ˆ˜

๐Ÿ“ Logic

for (String page : pages) { Page p = new Page(word, page); // url์„ ํ‚ค๋กœ ํ•˜์—ฌ ๋งต ์ถ”๊ฐ€ pageInfo.put(p.url, p); }
  • Page ํด๋ž˜์Šค์˜ setScore(), setExtPages() , setLinkScore() ๋กœ ๊ฐ page ์— ๋Œ€ํ•ด ๊ธฐ๋ณธ ์ ์ˆ˜์™€ ์™ธ๋ถ€ ๋งํฌ ์ˆ˜, ๋งํฌ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
for (Page p : pageInfo.values()) { for (String extPage : p.extPages) { if (pageInfo.containsKey(extPage)) pageInfo.get(extPage).addScore(p.getLinkScore()); } }
  • ํ•ด๋‹น ์›น ํŽ˜์ด์ง€๋กœ ๋งํฌ๊ฐ€ ๊ฑธ๋ฆฐ ๋‹ค๋ฅธ ์›น ํŽ˜์ด์ง€์˜ ๋งํฌ ์ ์ˆ˜(๊ธฐ๋ณธ ์ ์ˆ˜ / ์™ธ๋ถ€ ๋งํฌ ์ˆ˜)์˜ ์ดํ•ฉ์„ ๊ตฌํ•ด ํ•ด๋‹น ์›น ํŽ˜์ด์ง€์˜ ๋งค์นญ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

๐Ÿ“„ CODE

์ฝ”๋“œ ๋ณด๊ธฐ/์ ‘๊ธฐ๐Ÿ’ซ
import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; class Page { String url; double score = 0; double linkScore = 0; ArrayList<String> extPages; public Page(String word, String page) { this.url = getUrl(page); extPages = new ArrayList<>(); setScore(page, word); setExtPages(page); setLinkScore(); } private String getUrl(String page) { Pattern pattern = Pattern.compile("<meta property=\"og:url\" content=\"(\\S*)\""); Matcher matcher = pattern.matcher(page); if (matcher.find()) return matcher.group().split("=")[2].replaceAll("\"", ""); return ""; } public void addScore(double score) { this.score += score; } public double getScore() { return score; } private void setScore(String page, String word) { // body ํƒœ๊ทธ ๋‚ด ๋ชจ๋“  ์ˆซ์ž๋“ค์„ ๊ณต๋ฐฑ์œผ๋กœ replaceํ•˜์—ฌ, word๋ฅผ ์ฐพ๊ธฐ ์‰ฝ๋„๋ก ํ•จ String body = page.split("<body>")[1].split("</body>")[0].replaceAll("\\d", " "); Pattern pattern = Pattern.compile("\\b(?i)" + word + "\\b"); Matcher matcher = pattern.matcher(body); while(matcher.find()) score++; } private void setExtPages(String page) { Pattern pattern = Pattern.compile("<a href=\"https://(\\S*)\""); Matcher matcher = pattern.matcher(page); while(matcher.find()) extPages.add(matcher.group().split("\"")[1]); } public double getLinkScore() { return linkScore; }5 private void setLinkScore() { this.linkScore = this.score / extPages.size(); } } class Solution { public int solution(String word, String[] pages) { word = word.toLowerCase(); Map<String, Page> pageInfo = new LinkedHashMap<>(); for (String page : pages) { Page p = new Page(word, page); // url์„ ํ‚ค๋กœ ํ•˜์—ฌ ๋งต ์ถ”๊ฐ€ pageInfo.put(p.url, p); } for (Page p : pageInfo.values()) { for (String extPage : p.extPages) { if (pageInfo.containsKey(extPage)) pageInfo.get(extPage).addScore(p.getLinkScore()); } } double maxScore = 0; int maxIdx = 0; int idx = 0; for (Map.Entry<String, Page> entry : pageInfo.entrySet()) { double score = entry.getValue().getScore(); if (score > maxScore) { maxScore = score; maxIdx = idx; } idx++; } return maxIdx; } }

โœ๏ธ Review

  • ๋ฌธ์ œ ์ž์ฒด๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ–ˆ์ง€๋งŒ ๊ตฌํ˜„์ด ์ง„์งœ ๋”๋Ÿฌ์› ๋˜ ๋ฌธ์ œ ... ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์ง€ ์•Š์œผ๋ฉด ์ ˆ๋Œ€ ๊ทธ๋ƒฅ ๋ชป ํ’€ ๋“ฏ ํ•˜๋‹ค..
  • ๋ฌธ์ œ ์ดํ•ด๋ฅผ ์ž˜๋ชปํ•ด์„œ, ํ•ด๋‹น ์›น ํŽ˜์ด์ง€๋กœ ๋งํฌ๊ฐ€ ๊ฑธ๋ฆฐ ๋‹ค๋ฅธ ์›นํŽ˜์ด์ง€์˜ ๊ธฐ๋ณธ ์ ์ˆ˜ / ์™ธ๋ถ€ ๋งํฌ์˜ ์ˆ˜์˜ ์ดํ•ฉ์„ ํ•ด๋‹น ์›น ํŽ˜์ด์ง€๋กœ ๋งํฌ๊ฐ€ ๊ฑธ๋ฆฐ ๋‹ค๋ฅธ ์›น ํŽ˜์ด์ง€์˜ ๊ธฐ๋ณธ ์ ์ˆ˜์™€ ํ˜„์žฌ ํŽ˜์ด์ง€์˜ ๋งํฌ์˜ ์ˆ˜์— ๋Œ€ํ•œ ๊ณ„์‚ฐ์œผ๋กœ ์ฐฉ๊ฐํ•ด์„œ ์‹œ๊ฐ„์„ ๋” ๋‚ ๋ ธ๋‹ค.
@Seongukbaek
๋ฐฐ์šฐ๊ณ  ์ •๋ฆฌํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ๋Œ€ํ•™์ƒ ๋ฐฑ์„ฑ์šฑ์ž…๋‹ˆ๋‹ค ๐Ÿ˜€